<?php

namespace app\api\controller\worker;

use app\api\controller\Base;
use app\common\model\AdminModel;
use app\common\model\node\NodeModel;
use app\common\model\worker\CustomerAuthModel;
use app\common\model\worker\CustomerModel;
use app\common\model\worker\TaskModel;
use think\facade\View;

class CustomerAuthController extends Base
{

    private $model;
    private $customerModel;
    private $nodeModel;
    private $taskModel;
    private $adminModel;
    /**
     * 构造方法
     * @access public
     * @throws \Exception
     */
    public function __construct()
    {

        parent::__construct();
        $this->checkLogin();
        $this->model = new CustomerAuthModel();
        $this->customerModel = new CustomerModel();
        $this->nodeModel = new NodeModel();
        $this->taskModel = new TaskModel();
        $this->adminModel = new AdminModel();

    }

    /**
     * 客户任务指派
     */
    public function getAuth(){
        $customerId = $this->request->param('customer_id');
        $orderId = $this->request->param('order_id',0);
        $authList = $this->model->where('customer_id',$customerId)->column('*','node_id');
        $nodeList = $this->nodeModel->getNodeList(1);//客户节点
        $customerInfo = $this->customerModel->getInfo(['id'=>$customerId]);
        $adminList = $this->adminModel->column('id,mobile,nickname,status','id');
        foreach ($nodeList as $k=>$v){
            $v['admin_id'] = isset($authList[$v['id']])?$authList[$v['id']]['admin_id']:0;
            $v['admin_nickname'] = $v['admin_id']>0?$adminList[$v['admin_id']]['nickname']:'';
            $nodeList[$k] = $v;
        }
        $data = [
            'customer_info'=>$customerInfo,
            'node_list'=>$nodeList,
            'admin_list'=>$adminList
        ];
        $this->result($data);
    }

    public function setAuth(){
        $data = $this->request->param();
        $info = $this->model->appointAuth($data['customer_id'],$data['order_id'],$data['admin'],$this->loginInfo['id']);
        $this->result($info);
    }

    public function addAuth(){
        $data = $this->request->param();
        $this->validate($data,[
            'customer_id|客户id'=>'require',
            'order_id|订单id'=>'require',
            'node_id|任务节点'=>'require',
            'admin_id|执行人'=>'require',
        ]);
        $authInfo = $this->model->where('customer_id',$data['customer_id'])
            ->where('order_id',$data['order_id'])
            ->where('node_id',$data['node_id'])
            ->find();
        if(!empty($authInfo)){
            $this->model->where('id',$authInfo['id'])->update(['admin_id'=>$data['admin_id']]);
        }else{
            $this->model->insert($data);
        }
        $this->result('ok');


    }



}
